package se.teamscape.client.controller;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.PlaceManager;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.place.PlaceRequestEvent;
import se.teamscape.client.I18N.TeamScapeConstants;
import se.teamscape.client.I18N.TeamScapeMessages;
import se.teamscape.client.gin.TeamScapeGinjector;
import se.teamscape.client.place.WelcomePlace;
import se.teamscape.client.presenter.AppPresenter;
import se.teamscape.client.presenter.BaseContainerPresenter.WidgetProperties;
import se.teamscape.client.view.AppView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * AppController will be our app control point where we bind event handlers to
 * events, setup I18N stuff, etc.
 * 
 * @author Andreas Borglin
 * 
 */
public class AppController {

    // I18N constants/messages instances
    private final TeamScapeConstants mConstants = GWT.create(TeamScapeConstants.class);
    private final TeamScapeMessages mMessages = GWT.create(TeamScapeMessages.class);
    private TeamScapeGinjector mInjector;

    private static AppController sAppController;

    public static AppController getInstance() {

        if (sAppController == null)
            sAppController = new AppController();

        return sAppController;
    }

    public static TeamScapeConstants getConstants() {
        return getInstance().mConstants;
    }

    public static TeamScapeMessages getMessages() {
        return getInstance().mMessages;
    }

    public static TeamScapeGinjector getInjector() {
        return getInstance().mInjector;
    }

    private AppController() {

    }

    public void go(TeamScapeGinjector injector) {

        mInjector = injector;
        // Fetch needed instances from injector
        final EventBus eventBus = mInjector.getEventBus();
        final PlaceManager placeManager = mInjector.getPlaceManager();
        final AppPresenter appPresenter = mInjector.getAppPresenter();

        // Setup AppView containers
        // TODO: The idea here is that we will define the application layout
        // here in one place
        // We assign presenters direction and size and will also do css and
        // stuff later
        mInjector.getNorthPresenter().setWidgetProperties(new WidgetProperties(AppView.NORTH, 0, 100));
        mInjector.getSouthPresenter().setWidgetProperties(new WidgetProperties(AppView.SOUTH, 0, 50));
        mInjector.getWestPresenter().setWidgetProperties(new WidgetProperties(AppView.WEST, 0, 150));
        mInjector.getEastPresenter().setWidgetProperties(new WidgetProperties(AppView.EAST, 0, 50));

        // Must set widget properties before calling this!
        appPresenter.revealAll();

        // Add AppPresenter's display to the root panel
        RootLayoutPanel.get().add(appPresenter.getDisplay().asWidget());

        // If there are no tokens on history stack,
        // launch initial state token provided as parameter
        if (History.getToken().length() == 0) {
            eventBus.fireEvent(new PlaceRequestEvent(new PlaceRequest(WelcomePlace.NAME)));
        }
        // We have a token, so let's fire off an event and handle it
        else {
            placeManager.fireCurrentPlace();
        }

    }
}
